//
//  JAMainController.m
//  网易新闻
//
//  Created by Arvin on 17/3/18.
//  Copyright © 2017年 Arvin. All rights reserved.
//

#import "JAMainController.h"

@implementation JAMainController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addChildController];
}

//添加所有的子控制器
-(void)addChildController{
    
    NSArray *childPage = @[
                           @{@"clsName":@"JAHomeViewController",@"title":@"首页",@"icon":@"news"},
                           @{@"clsName":@"UIViewController",@"title":@"阅读",@"icon":@"reader"},
                           @{@"clsName":@"UIViewController",@"title":@"视频",@"icon":@"media"},
                           @{@"clsName":@"UIViewController",@"title":@"话题",@"icon":@"found"},
                            @{@"clsName":@"UIViewController",@"title":@"我",@"icon":@"me"}
                           ];
    
    NSMutableArray *childdren = [NSMutableArray array];
    
    //将创建的字典添加到可变数数组
    for (NSDictionary *dic in childPage) {
        UINavigationController *child = [self childController:dic];
        
        [childdren addObject:child];
    }
    
    self.viewControllers = childdren;
}

//创建单个控制器
-(UINavigationController *)childController:(NSDictionary *)dic{
    
    NSString *clsName = dic[@"clsName"];
    
    //获取子控制器的类名,生成子控制器
    UIViewController *controller = [[NSClassFromString(clsName) alloc]init];
    
    //设置子控制器的标题
    controller.title = dic[@"title"];
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
    
    //设置子控制器的tabBarItem的图片和选中图片
    //图片正常状态
    UIImage *image = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_icon_%@_normal",dic[@"icon"]]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.image = image;
    
    //图片选中状态
    UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_icon_%@_highlight",dic[@"icon"]]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = selectedImage;
    
    //把子控制器设为navgitaionController的根控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
    
    return nav;
}
@end
